package com.albatross.rest;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.Response;

import services.VulnerabilityService;
import services.VulnerabilityServiceImpl;
import beans.VulnerabilityMaster;

import com.apple.ist.ets.arc.albatross.common.utils.UUIDGenerator;

@Path("/hello")
public class VulnerabilityRestService {
	VulnerabilityService service = new VulnerabilityServiceImpl();

	@GET
	@Path("/{param}")
	public Response getMsg(@PathParam("param") String msg) {

		String output = "Jersey say : " + msg;

		return Response.status(200).entity(output).build();

	}

	@GET
	@Path("/create")
	public Response create() throws Exception {
		VulnerabilityMaster resource = null;
		//System.out.println("inside create method");
		VulnerabilityDataSource t = new VulnerabilityDataSource();
		System.out.println("Before service");
		 //getting current date and time using Date class
	       DateFormat df1 = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
	       Date dateobj1 = new Date();
	       System.out.println("time taken"+df1.format(dateobj1));	
		String a = t
				.callURL("http://isdb-api-test.corp.apple.com/api/iterator/vulnerability?show=100");
		
		List<VulnerabilityMaster> li = new ArrayList<VulnerabilityMaster>();
		li = t.getVulEntity(a);
		
		//List<VulnerabilityMaster> li_new = new ArrayList<VulnerabilityMaster>();

		for(int loop=100;loop<200;loop=loop+100){
			
			String temp="http://isdb-api-test.corp.apple.com/api/iterator/vulnerability?show=100&skip=";
			
			temp=temp.concat(Integer.toString(loop));
			
			String a1 = t
					.callURL(temp);
			
			li.addAll(t.getVulEntity(a1));
			
		}
		
		
	
		for (int i = 0; i < li.size(); i++) {
			resource = li.get(i);
			resource.setAppId(li.get(i).getAppId());
			resource.setAppIp(li.get(i).getAppIp());
			resource.setHostPort(li.get(i).getHostPort());
			resource.setVulId(UUIDGenerator.getUUID());
			resource.setLastFixedDateTime(li.get(i).getLastFixedDateTime());
			resource.setLastFoundDateTime(li.get(i).getLastFoundDateTime());
			resource.setLastServiceModDateTime(li.get(i)
					.getLastServiceModDateTime());
			resource.setLastTestDateTime(li.get(i).getLastTestDateTime());
			resource.setLastUpdateDateTime(li.get(i).getLastUpdateDateTime());

			resource.setPatchable(li.get(i).getPatchable());
			resource.setPciFlag(li.get(i).getPciFlag());
			resource.setProtocol(li.get(i).getProtocol());
			resource.setPublishedDateTime(li.get(i).getPublishedDateTime());
			resource.setQualisId(li.get(i).getQualisId());
			resource.setQualisTickets(li.get(i).getQualisTickets());
			resource.setQualisType(li.get(i).getQualisType());

			resource.setSeverityLevel(li.get(i).getSeverityLevel());
			resource.setCrawlId(li.get(i).getCrawlId());

		//resource.setCreatedAt(li.get(i).getCreatedAt());
			resource.setCreatedAtMS(li.get(i).getCreatedAtMS());
			resource.setDaysToLive(li.get(i).getDaysToLive());
			resource.setFailOnEmpty(li.get(i).getFailOnEmpty());
			resource.setFirstseen(li.get(i).getFirstseen());
			resource.setHash(li.get(i).getHash());
		resource.setIsHashChanged(li.get(i).getIsHashChanged());
			resource.setIsNew(li.get(i).getIsNew());

			resource.setName(li.get(i).getName());
			resource.setTimeseen(li.get(i).getTimeseen());
			resource.setStatus(li.get(i).getStatus());
			resource.setTicketNumber(li.get(i).getTicketNumber());
			resource.setTitle(li.get(i).getTitle());
			resource.setType(li.get(i).getType());
			resource.setVulnerabilityType(li.get(i).getVulnerabilityType());

			service.create(resource);

		}

		/*
		 * resource = new VulnerabilityMaster(); resource.setVulId("1");
		 * resource.setAppId("1"); resource.setAppIp("1");
		 * resource.setHostPort(0);
		 */
		// dao.addVulnerability(resource);
		System.out.println("After service");
		 //getting current date and time using Date class
	       DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
	       Date dateobj = new Date();
	       System.out.println("time taken"+df.format(dateobj));	
	       
	       
	       String output = "Insertion of Data done";

		return Response.status(200).entity(output).build();

	}

	@GET
	@Path("/vulData")
	public Response getUsersAll() {
		List<VulnerabilityMaster> vls = null;
		vls = service.getAllVulnerabilities();

		GenericEntity<List<VulnerabilityMaster>> entity = new GenericEntity<List<VulnerabilityMaster>>(
				vls) {
		};
		return Response.ok(entity).build();
		// return vls;

	}

	
	
	
	
}